
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;


import BE.Song;
import BLL.SearchManager;
import BLL.SongManager;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 *
 * @author agnarsdottir
 */
public class SongSearchManagerJForm extends javax.swing.JFrame
{

    private final SongManager mgr;
    private final SongTableModel songModel;
    private final SongTable tblSongs;
    private final SearchManager smg;
    
    

    /**
     * Creates new form GUI
     */
    public SongSearchManagerJForm()
    {
        initComponents();
        // Center the frame on the screen.
        setLocationRelativeTo(null);

        mgr = SongManager.getInstance();
        smg = new SearchManager();
        songModel = new SongTableModel(mgr.getAll());

        //Creating the JTable (SongTable) instance and adding it to the 
        // center of the panel.
        tblSongs = new SongTable(songModel);
        pnlJTable.add(new JScrollPane(tblSongs), BorderLayout.CENTER);

        //Necessary for rendering checkboxes for booleans in NIMBUS L&F
        // ((JComponent) tblSongs.getDefaultRenderer(Boolean.class)).setOpaque(true);
        tblSongs.getSelectionModel().addListSelectionListener(new ListSelectionListener()
        {
            @Override
            public void valueChanged(ListSelectionEvent lse)
            {
                if (!lse.getValueIsAdjusting())
                {
                    if (tblSongs.getSelectedRow() != -1)
                    {

                    }
                }

            }
        });

        //Table model listener - listening for changes in the table model.
        songModel.addTableModelListener(new TableModelListener()
        {
            @Override
            public void tableChanged(TableModelEvent tme)
            {
                // Clearing and setting back the selection triggers the 
                // selection listener of the JTable 
                tblSongs.clearSelection();
                tblSongs.setRowSelectionInterval(tme.getFirstRow(), tme.getFirstRow());
            }
        });
    }

    /**
     * Responsible for updating the values in the song details panel when a row
     * is selected in the JTable.
     */
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        buttonGroup1 = new javax.swing.ButtonGroup();
        lblQuery = new javax.swing.JLabel();
        txtSearchBar = new javax.swing.JTextField();
        btnSearchButtom = new javax.swing.JButton();
        lblResult = new javax.swing.JLabel();
        scScrollbar = new javax.swing.JScrollBar();
        btnClose = new javax.swing.JButton();
        lblCount = new javax.swing.JLabel();
        lblLimit = new javax.swing.JLabel();
        btnClear = new javax.swing.JToggleButton();
        btnGroup = new javax.swing.JPanel();
        rbBeginsWith = new javax.swing.JRadioButton();
        rbContains = new javax.swing.JRadioButton();
        rbEndsWith = new javax.swing.JRadioButton();
        rbExact = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        cbArtist = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        cbGenre = new javax.swing.JComboBox();
        pnlJTable = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblQuery.setText("Query:");

        txtSearchBar.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                txtSearchBarActionPerformed(evt);
            }
        });

        btnSearchButtom.setText("Search");
        btnSearchButtom.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnSearchButtomActionPerformed(evt);
            }
        });

        lblResult.setText("Result:");

        btnClose.setText("Close");
        btnClose.setToolTipText("");
        btnClose.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnCloseActionPerformed(evt);
            }
        });

        lblCount.setText("Count:");

        lblLimit.setText("Limit:");
        lblLimit.setToolTipText("");

        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnClearActionPerformed(evt);
            }
        });

        btnGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Search Type"));

        buttonGroup1.add(rbBeginsWith);
        rbBeginsWith.setText("Begins With");
        rbBeginsWith.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                rbBeginsWithActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbContains);
        rbContains.setText("Contains");
        rbContains.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                rbContainsActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbEndsWith);
        rbEndsWith.setText("Ends With");
        rbEndsWith.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                rbEndsWithActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbExact);
        rbExact.setText("Exact");
        rbExact.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                rbExactActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout btnGroupLayout = new javax.swing.GroupLayout(btnGroup);
        btnGroup.setLayout(btnGroupLayout);
        btnGroupLayout.setHorizontalGroup(
            btnGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(btnGroupLayout.createSequentialGroup()
                .addGroup(btnGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbBeginsWith)
                    .addComponent(rbContains)
                    .addComponent(rbEndsWith)
                    .addComponent(rbExact))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        btnGroupLayout.setVerticalGroup(
            btnGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(btnGroupLayout.createSequentialGroup()
                .addComponent(rbBeginsWith)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbContains)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbEndsWith)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbExact, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 7, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Artist"));

        cbArtist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", " ", " " }));
        cbArtist.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cbArtistActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cbArtist, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(146, 146, 146))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbArtist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Genre"));

        cbGenre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "fokhagymas", "Item 2", "Item 3", "Item 4" }));
        cbGenre.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cbGenreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbGenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbGenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pnlJTable.setLayout(new java.awt.BorderLayout());

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "10", "20", "50", "100" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblResult)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSearchBar, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSearchButtom, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 227, Short.MAX_VALUE))
                                    .addComponent(pnlJTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(lblCount)
                                        .addGap(18, 18, 18)
                                        .addComponent(lblLimit))
                                    .addComponent(btnClose, javax.swing.GroupLayout.Alignment.LEADING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btnGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                            .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, 0)))
                .addComponent(scScrollbar, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSearchBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblQuery, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSearchButtom))
                        .addGap(26, 26, 26)
                        .addComponent(btnGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(lblResult)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pnlJTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblLimit)
                                .addComponent(lblCount)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addComponent(btnClose)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(scScrollbar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        lblQuery.getAccessibleContext().setAccessibleName("LBL");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSearchBarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txtSearchBarActionPerformed
    {//GEN-HEADEREND:event_txtSearchBarActionPerformed
        smg.searchValue(txtSearchBar.getText());
    }//GEN-LAST:event_txtSearchBarActionPerformed

    private void btnSearchButtomActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSearchButtomActionPerformed
    {//GEN-HEADEREND:event_btnSearchButtomActionPerformed
        ArrayList<Song> sngList = new ArrayList<>();
        sngList = smg.searchAction();
        jTextArea1.setText(sngList.toString());
    }//GEN-LAST:event_btnSearchButtomActionPerformed

    private void cbArtistActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cbArtistActionPerformed
    {//GEN-HEADEREND:event_cbArtistActionPerformed
        smg.getArtistSelection(cbArtist.getSelectedItem());  // TODO add your handling code here:
                
                
    }//GEN-LAST:event_cbArtistActionPerformed

    private void cbGenreActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cbGenreActionPerformed
    {//GEN-HEADEREND:event_cbGenreActionPerformed
       smg.getGenreSelection(cbGenre.getSelectedItem()); // TODO add your handling code here:
       
    }//GEN-LAST:event_cbGenreActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCloseActionPerformed
    {//GEN-HEADEREND:event_btnCloseActionPerformed
        dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void rbBeginsWithActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_rbBeginsWithActionPerformed
    {//GEN-HEADEREND:event_rbBeginsWithActionPerformed
       rbBeginsWith.setEnabled(false);
    }//GEN-LAST:event_rbBeginsWithActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jComboBox1ActionPerformed
    {//GEN-HEADEREND:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClearActionPerformed
    {//GEN-HEADEREND:event_btnClearActionPerformed
        
    }//GEN-LAST:event_btnClearActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnClear;
    private javax.swing.JButton btnClose;
    private javax.swing.JPanel btnGroup;
    private javax.swing.JButton btnSearchButtom;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbArtist;
    private javax.swing.JComboBox cbGenre;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCount;
    private javax.swing.JLabel lblLimit;
    private javax.swing.JLabel lblQuery;
    private javax.swing.JLabel lblResult;
    private javax.swing.JPanel pnlJTable;
    private javax.swing.JRadioButton rbBeginsWith;
    private javax.swing.JRadioButton rbContains;
    private javax.swing.JRadioButton rbEndsWith;
    private javax.swing.JRadioButton rbExact;
    private javax.swing.JScrollBar scScrollbar;
    private javax.swing.JTextField txtSearchBar;
    // End of variables declaration//GEN-END:variables
}
